package com.word.app.controller;


import java.util.*;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.word.app.controller.response.BaseResponse;
import com.word.app.pojo.Question;
import com.word.app.pojo.dto.PagingReq;
import com.word.app.service.IQuestionService;

/**
 * @author nano
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    IQuestionService service;

    @RequiresPermissions(value={"perm:root", "perm:visitor"}, logical = Logical.OR)
    @PostMapping("/query")
    public BaseResponse query(@RequestBody PagingReq<Question> req) {
        QueryWrapper<Question> wrapper = new QueryWrapper<Question>();
        Question question = req.getParams();
        if(question!=null && question.getContent()!=null){
            wrapper.like("content", String.format("%%%s%%", question.getContent()));
            question.setContent(null);
        }
        wrapper.setEntity(question);
        
        return new BaseResponse().data(service.page(req.getPage(), wrapper));
    }

    // 考试随机选取考题
    @RequiresPermissions(value={"perm:root", "perm:visitor"}, logical = Logical.OR)
    @PostMapping("/query/random")
    public BaseResponse queryRandom(@RequestBody PagingReq<Question> req) {
        return new BaseResponse().data(service.queryRandom(req.getPage(), req.getParams()));
    }

    @RequiresPermissions(value={"perm:root", "perm:visitor"}, logical = Logical.OR)
    @PostMapping("/query/result")
    public BaseResponse queryResult(@RequestBody List<Integer> req) {
        return new BaseResponse().data(service.listByIds(req));
    }
    
    @RequiresPermissions(value={"perm:root", "perm:visitor"}, logical = Logical.OR)
    @GetMapping("/query/count")
    public BaseResponse queryTypeCount(@RequestParam Integer id){
        return new BaseResponse().data(service.getCount(id));
    }

    @RequiresPermissions(value="perm:root")
    @PostMapping("/add")
    public void add(@RequestBody Question question){
        service.save(question);
    }

    @RequiresPermissions(value="perm:root")
    @PostMapping("/delete")
    public void delete(@RequestBody List<String> ids){
        service.removeByIds(ids);
    }

    @RequiresPermissions(value="perm:root")
    @PostMapping("/update")
    public void update(@RequestBody Question question) {
        service.updateById(question);
    }

}
